/*
Content: Alternative specification with rolling sums and lags
Replicates: Table 5
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

	
*********************************
***** DAILY EVENTS ************
*********************************

use  "$dataevent/allevents_daily_soep_final.dta", clear 
* For each country in the data expand to have a panel month-year for 1990 to 2018
drop if year<1990
tab year
drop if startdate==.
keep soep_corigin_id
bys soep_corigin_id: gen unique = _n
keep if unique == 1 

expand 10950 // (2019-1990+1)*365=10950

* Create variable startdate for all day-month-year combinations from 1990 to 2019	
	gen counter = 1 
	bys soep_corigin_id: gen startdate = 10958	// 120 is Jan1970 in the month-year number classification
	bys soep_corigin_id: gen spell = _n - 1 
	bys soep_corigin_id: replace startdate = startdate + spell 
	format startdate %td		

merge 1:1 startdate soep_corigin_id using "$dataevent/allevents_daily_soep_final.dta", gen(_merge_terror) keepusing(soep_corigin_id startdate terror  year month ) keep(1 3)

cap drop month
cap drop year
cap drop day
	 g month=month(dofm(startdate))
	 g year=year(startdate)
	 g day=day(startdate)


sort soep_c startdate 

* If in a given date there is no event assign a zero	
foreach var in  terror     {
replace `var' = 0 if _merge_terror == 1 // months with no terror

}
	
	
********************************************************************************	
** TREATMENT: ROLLING SUMS OF TERROR EVENTS BEFORE INTERVIEW
********************************************************************************
* Cummulative	 
drop _merge_terror unique counter spell


* Total number of attacks by timing before interview
sort soep_corigin_id startdate
foreach event in terror  {
foreach day in 15 30 90 {
local start=`day'+1
qui: rangestat (sum) cum_`event'_`day'd=`event', interval(startdate,-`start',-1) by(soep_corigin_id)

}
}


* Taking logs
foreach var in   terror     {
foreach day in 15 30 90 {
local start=`day'+1
g ln_cum_`var'_`day'd=ln(cum_`var'_`day'd+1)
}
}


* At least one conflict in the past x days 
foreach event in  terror     {	
foreach day in 15 30 90 {
qui: g	atleast1`event'_`day'd=cum_`event'_`day'd>=1 
}
}
	
	
********************************************************************************	
**CONTROL: ROLLING SUMS OF TERROR EVENTS OVER LAGGEG PERIODS
********************************************************************************	

* Total number of terrorist attacks by timing before interview

cap drop *90s
sort soep_corigin_id startdate
foreach var in terror    {
foreach day in 365 {
foreach t in 90 30 15 {

local end = `t' +1
local start= `end' + `day' 

qui: rangestat (sum) cum_`var'_`day'l_`t's=`var', interval(startdate,-`start',-`end') by(soep_corigin_id)
}
}

}


* Taking logs

sort soep_corigin_id startdate
foreach var in   terror     {
foreach day in 365 {
foreach t in 90 30 15 {
g ln_cum_`var'_`day'l_`t's=ln(cum_`var'_`day'l_90s+1)
g cum_`var'_`day'l_`t's_d=(cum_`var'_`day'l_90s>0)
replace cum_`var'_`day'l_`t's_d=. if cum_`var'_`day'l_90s==.
}
}
}


* Drop observations before 1987
cap drop month
g month=month(dofm(startdate))
drop if year<1987

	
		
compress
save "$globalterror\terror_dailydata_rolling_final.dta", replace 
